package com.ruoyi.web.zangchat.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.text.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.zangchat.domain.ChatUsers;
import com.ruoyi.web.zangchat.service.IChatUsersService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户Controller
 * 
 * @author wudi
 * @date 2020-07-19
 */
@Controller
@RequestMapping("/zangchat/users")
public class ChatUsersController extends BaseController
{
    private String prefix = "zangchat/users";

    @Autowired
    private IChatUsersService chatUsersService;

    @RequiresPermissions("zangchat:users:view")
    @GetMapping()
    public String users()
    {
        return prefix + "/users";
    }

    /**
     * 查询用户列表
     */
    @RequiresPermissions("zangchat:users:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ChatUsers chatUsers)
    {
        startPage();
        return getDataTable(chatUsersService.list(new QueryWrapper<ChatUsers>(chatUsers)));
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("zangchat:users:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChatUsers chatUsers)
    {
        List<ChatUsers> list = chatUsersService.list(new QueryWrapper<>());
        ExcelUtil<ChatUsers> util = new ExcelUtil<ChatUsers>(ChatUsers.class);
        return util.exportExcel(list, "users");
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @RequiresPermissions("zangchat:users:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ChatUsers chatUsers)
    {
        return toAjax(chatUsersService.save(chatUsers));
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        ChatUsers chatUsers = chatUsersService.getById(id);
        mmap.put("chatUsers", chatUsers);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("zangchat:users:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChatUsers chatUsers)
    {
        return toAjax(chatUsersService.updateById(chatUsers));
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("zangchat:users:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(chatUsersService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
